package cn.iocoder.yudao.module.bpm.util;

import cn.hutool.cache.CacheUtil;
import cn.hutool.cache.impl.TimedCache;
import cn.hutool.db.meta.Column;
import cn.hutool.db.meta.MetaUtil;
import cn.hutool.db.meta.Table;

import javax.sql.DataSource;
import java.util.Collection;

/**
 * @ClassName TableUtil
 * @Description TODO
 * @Author dingyongze
 * @Date 2024/6/11 23:17
 * @Version 1.0
 */
public class TableUtil {
    private static TimedCache<String, Collection<Column>> timedColumnCache = CacheUtil.newTimedCache(120*1000);

    public static Collection<Column> getColumns(String tableName) {
        //获取数据库表字段
        DataSource datasource = DatasourceUtil.getDataSource("master");
        Collection<Column> tableMetaColumns = timedColumnCache.get(tableName);
        if (tableMetaColumns == null) {
            Table tableMeta = MetaUtil.getTableMeta(datasource, tableName);
            tableMetaColumns = tableMeta.getColumns();
            timedColumnCache.put(tableName, tableMetaColumns);
        }
        return tableMetaColumns;
    }
}
